from contextlib import contextmanager

import pyodbc 
from pyodbc import Connection, Cursor 

from .config import get_settings 

settings = get_settings()

@contextmanager
def create_curs() : 
    conn: Connection | None= None 
    curs: Cursor | None = None 
    try: 
         conn = pyodbc.connect(settings.conn_str) 
         curs = conn.cursor()
         yield curs 
         conn.commit()

    except Exception as e: 
         if conn:
            conn.rollback()
         raise 
    finally: 
         if curs: 
              curs.close()
         if conn: 
              conn.close()


         
         

    